package com.zhanyd.app.controller;

import com.alibaba.fastjson.JSONObject;
import com.zhanyd.app.common.ApiResult;
import com.zhanyd.app.common.ResultEnum;
import com.zhanyd.app.common.WeixinHelper;
import com.zhanyd.app.common.util.HttpService;
import com.zhanyd.app.common.util.JwtUtils;
import com.zhanyd.app.common.util.MD5Generate;
import com.zhanyd.app.common.util.StringHelp;
import com.zhanyd.app.model.UserInfo;
import com.zhanyd.app.service.UserService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import java.util.*;


/**
 * Created by zhanyd@sina.com on 2018/2/13 0013.
 */
@RestController
@EnableAutoConfiguration
@RequestMapping("/user")
public class UserController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    UserService userInfoService;
    
    /**
             * 用户登录
     * @param userName
     * @param password
     * @return
     */
    @RequestMapping("/login")
    public ApiResult<Map<String, Object>> login(String userName,String password){
    	ApiResult<Map<String, Object>> apiResult = new ApiResult<Map<String, Object>>();
    	Map<String, Object> resultMap = new HashMap<String, Object>();

		Map<String,String> param = new HashMap<String,String>();
		param.put("userName", userName);
		//MD5加密
		param.put("password", MD5Generate.getMD5(password.getBytes()));
		List<Map<String,Object>> userList = userInfoService.selectByParam(param);
		//判断该用户是否已经存在
		if(!userList.isEmpty()){
			//生成token
			String token = JwtUtils.signJWT(StringHelp.parseInt(userList.get(0).get("id")));
			if(token == null){
				resultMap.put("msg", "生成token失败");
				return apiResult.fail(resultMap);
			}else{
				logger.info(userName + " 登录成功");
				resultMap.put("userInfo", userList.get(0));
				resultMap.put("token", token);
				return apiResult.success(resultMap);
			}
		}
		
		resultMap.put("msg", "用户名或密码错误");
		logger.info(userName + " 用户名或密码错误");
		return new ApiResult(500,"用户名或密码错误",null);
    }
    
    
    /**
            * 获取用户角色数组，用以权限控制
     * @param request
     * @return
     */
    @RequestMapping("/info")
    public ApiResult<Map<String, Object>> info(HttpServletRequest request){
    	System.out.println(JwtUtils.getUserId(request));
    	ApiResult<Map<String, Object>> apiResult = new ApiResult<Map<String, Object>>();
    	Map<String, Object> resultMap = new HashMap<String, Object>();
    	String token = request.getHeader("Authorization");
		Integer userId = JwtUtils.verifyJWT(token);
		List<Map<String, Object>> roleMapList = userInfoService.selectRoleInfoByUserId(userId);
    	ArrayList<String> roleList = new ArrayList<String>();
    	if(!roleMapList.isEmpty()){
    		if("admin".equals(StringHelp.valueOf(roleMapList.get(0).get("role_name")))){
    			roleList.add("admin");
    		}
    		else {
    			roleList.add("developer");
    		}
    	}
    	resultMap.put("roles", roleList);
    	return apiResult.success(resultMap);
    }
    

    /**
     * 用户登出
     * @return
     */
    @RequestMapping("/logout")
    public ApiResult<String> logout(){
    	ApiResult<String> apiResult = new ApiResult<String>();
    	return apiResult.success("登出");
    }
   

}
